package com.czd.support.controller;

import com.czd.base.utils.WebUtils;
import com.czd.support.common.Constants;
import com.czd.support.model.Manager;
import com.czd.support.service.ManagerService;
import com.czd.support.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Author: GuSi
 * Create: GuSi (14-7-5 13:05)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class LoginController extends BaseController{
	@Autowired
	private ManagerService managerService;

	@RequestMapping(value = {"/login",""},method = RequestMethod.GET)
	protected String login(HttpServletRequest request,Model model)
	{
		Manager manager = this.getSesionUser(request);
		if(manager != null && manager.getUserCode() >0){
			return "redirect:/index";
		}
		return "login";
	}

	@RequestMapping(value = "/login",method = RequestMethod.POST)
	protected String logined(HttpServletRequest request,Model model) throws Exception
	{
		String userCode = WebUtils.findParamStr(request, "userCode");
		String password = MD5Utils.str2Md5(WebUtils.findParamStr(request, "password"));
		Manager manager = managerService.login(userCode,password);
		if(manager != null){
			request.getSession().setAttribute(Constants.SESSION_USER,manager);
			return  "redirect:/index";
		}else {
			model.addAttribute("msg","用户名密码不正确");
		}
		return "login";
	}

	@RequestMapping(value = "/logout",method = RequestMethod.GET)
	protected String logout(HttpServletRequest request) throws Exception {
		request.getSession().removeAttribute(Constants.SESSION_USER);
		return "login";
	}

	@RequestMapping(value = "/index",method = RequestMethod.GET)
	public String index(){
		return "index";
	}

}
